import scipy
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


class Point:
    """
    Each instance represent a signal point from Signal.split method.
    """
    number = 0

    def __init__(self, idx, value, time):
        Point.number += 1
        self.index = idx
        self.value = value
        self.time = time

    def property(self, **properties):
        self.properties = properties

class Signal:
    def __init__(self,filepath):
        self.origin = pd.read_excel(filepath)
        self.times=self.origin["Time (s)"]
        self.time0=self.origin["Time (s)"][0]
        self.values=self.origin["R (Ω)"]
        self.num=len(self.origin)
    def split(self, interval):
        """
        Select the points with the interval.
        :param interval: The number of values between two selected points.
        """
        self.interval=interval
        self.points_list=[None]*(self.num//interval+1)
        i=0
        for row in range(0,self.num,interval):
            value=self.values[row]
            time=self.times[row]
            self.points_list[i]=Point(row,value,time)
            i+=1
    def statistic(self):
        self.points_list[0].property(mean=self.points_list[0].value,gradint=0)
        self.properties={"mean":[],"gradient":[]}
        for i in range(1,(len(self.points_list))):
            mean=self.values[(i-1)*self.interval:i*self.interval].mean()
            interval=self.points_list[i].time-self.points_list[i-1].time
            intreval_s=interval.seconds+interval.microseconds*1e-6
            gradient=(mean-self.points_list[i-1].properties['mean'])/intreval_s
            self.points_list[i].property(mean=mean,gradint=gradient)
            self.properties["mean"].append(mean)
            self.properties["gradient"].append(gradient)
    def visualize(self,pattern):
        """
        Visualize the original signal or the properties.
        :param pattern: string, equal to origin or properties.
        :return:
        """
        if pattern=="origin":
            fig = plt.figure()
            plt.plot(self.times,self.signal)
        elif pattern=="properties":
            fig,axes=plt.subplots(2,1)
            axes[0].plot(range(len(self.properties["mean"])),self.properties["mean"])
            axes[0].set_title("Mean value of every points.")
            axes[1].plot(range(len(self.properties["gradient"])), self.properties["gradient"])
            axes[1].set_title("Gradient value of every points.")
        elif pattern=="time":
            pass
        fig.show()
